<?php
  $r = $_POST['a'];
  if($r == null || $r == ''){
		$r = $_SESSION['a'];
		unset($_SESSION['a']);
  }
  if( $r != null && $r != '' ){
	  $rank_result_display = 'block';
	  $rank_style1_display = 'none';
  }else{
	  $rank_result_display = 'none';
	  $rank_style1_display = 'block';
  }
?>

<html>
<head>

	<script type="text/javascript" src="jquery.js"></script>
	<script type="text/javascript"> 
	    $(document).ready(function(){
			$('#input_style1').click(function(){
				$('#rank_style1').slideToggle('slow');
			});
			$('#input_style2').click(function(){
				$('#rank_style2').slideToggle('slow');
			});
			$('#addTextbox').click(function(){
				var last = $('#textList li:last').index();
				last++;
				$('#textList').append('<li><input type="text" id="cve'+last+'"/></li>');
			});
			$('#delTextbox').click(function(){
				$('#textList li:last').remove();		
			});
		});
		function rankByText(){ 
			 var num = $('#textList li:last').index(); 
			 var CVEList='';
			 for(i=0;i<=num;i++){
				CVEList = CVEList + $('#cve'+i).val()+';';
			 }
			 document.ranktext.a.value = CVEList;
			 document.ranktext.submit();
		}
	</script>
	
	<style type="text/css"> 
		p.flip{
			margin:5px;
			padding:5px;
            font-weight:bold;
			background:#e5eecc;
			border:solid 2px #c3c3c3;
			cursor:pointer;
		}
                
       div.flip{
            display:none;
            margin:5px;
			padding:5px;
			background:#F5F5DC;
			border:solid 2px #c3c3c3;
        }	
	</style>

</head>
 
<body>

	<p>
		Please enter a list of CVEs you wish to rank, either by filling textboxes or uploading a text file.
	</p>

	<p id="input_style1" class="flip">
		Enter a list of CVEs by filling textboxes.
	</p>

	<div id="rank_style1" class="flip" style="display:<?php echo $rank_style1_display?>">
		Fill a CVE name in each entry. Add more or delete entries following your number of CVEs.<br/>
		For example: CVE-2010-3424<br/>
		  <fieldset>
			  <legend>CVE List</legend>
			  <form name="ranktext" method="post" action="">
					<ol id="textList">
						<?php
							for ($i=0; $i<3; $i++)
							  {
								echo '<li><input type="text" id="cve'.$i.'"/></li>';
							  }
						?>
					</ol>
					<input type="button" id="addTextbox" value="Add"/>
					<input type="button" id="delTextbox" value="Del"/>
					<br/><br/>
					<input type="reset" name="reset" value="Reset"/>
					<input type="submit" name="submit" value="Rank!" onClick="rankByText()"/>
					<input type="hidden" name="a" value=""/>
			  </form>
		  </fieldset>
	</div>

	<p id="input_style2" class="flip"> 
		Enter a list of CVEs by uploading a text file.
	</p>

	<div id="rank_style2" class="flip">
		Upload a text file (.txt) with your list of CVE names, divided by a new line.<br/>
		For example:<br/>
		CVE-2010-3842<br/>
		CVE-2009-5554<br/>
		CVE-2010-2536<br/>
		<fieldset>
		    <legend>CVE List</legend>
			<form method="post" enctype="multipart/form-data"  action="?q=node/16">  <!--to uploadfile-->
				<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
				Choose your text file: <input id="fileupload" type="file" name="fileupload" /><br />
				<input type="submit" value="Rank!"/>
			</form>
		</fieldset>
	</div>

	<div id="rank_result" class="flip" style="text-align:center;display:<?php echo $rank_result_display?>">
	<?php
		if($r!=null && $r!=''){
            global $global_ws_address;

			$context = stream_context_create(array(
				'http' => array(
					'timeout' => 10      // Timeout in seconds
				)
			)); 

			$url = $global_ws_address.'o=ranklist&p1='.$r;
			$result = @file_get_contents($url,0,$context);
			$result = json_decode($result, true);
			if( !is_array($result) ){
				echo 'No results.';
			}else{
				$chart = array(
				  '#chart_id' => 'result',
				  '#title' => chart_title(t('Rank Result'), '0000ee', 15),
				  '#type' => CHART_TYPE_BAR_V,
				  '#size' => chart_size(700, 200),
				  '#bar_size' => chart_bar_size(40,10),
				);

				$i = 0;
				$x_axis1 = '';
				$x_axis2 = '|';
			  
				foreach( $result as $index => $value ){
				  $chart['#data'][$value['cvename']] = t($value['total']);
				  if( $i % 2 == 0 )  $x_axis1 .= $value['cvename'].'||';
				  else  $x_axis2 .= $value['cvename'].'||';
				  $i++;
				}

				$chart['#mixed_axis_labels'][CHART_AXIS_X_BOTTOM][0][] = chart_mixed_axis_label(t($x_axis1));
				if( $i > 1 )
				  $chart['#mixed_axis_labels'][CHART_AXIS_X_BOTTOM][1][] = chart_mixed_axis_label(t($x_axis2));

				$max = ceil($result[0]['total']/50) * 50;

				$chart['#mixed_axis_labels'][CHART_AXIS_Y_LEFT][0][] = chart_mixed_axis_range_label(0, $max);
				$chart['#data_scale'] = array('#min' => 0, '#max' => $max);

				$chart['#shape_markers'][] = array('N','000000',0,-1,11);

				echo chart_render($chart);

				echo '<br/>';
				echo '<ul style="text-align:left">';
				echo 'Click to see details.';
				foreach($result as $index => $value){
					echo '<li><a href="?q=node/14&cve='.$value['cvename'].'">'.$value['cvename'].'</a></li>';
				}
				echo '</ul>';
			}
		}
	?>
	</div>

</body>
</html>
